﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using RaceScore.BusinessLayer;

namespace RaceScore
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();

            label1.Visible = false;
            label2.Visible = false;
            label3.Visible = false;
            label4.Visible = false;
            label5.Visible = false;
        }

        public void InputBibsMessage(string msg)
        {
            label4.Text = msg;
            label4.Visible = true;
        }

        public void ResultsMessage(string msg)
        {
            label5.Text = msg;
            label5.Visible = true;
        }

        public void AddRaceMessage(string msg)
        {
            label1.Text = msg;
            label1.Visible = true;
        }


        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void newRaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form AddRace = new AddRace(this);
            AddRace.Show();
        }

        private void Main_Load(object sender, EventArgs e)
        {

        }

        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            // Insert code to read the stream here.
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }

                label2.Visible = true;
                label2.Text = "20 participants imported"; 
            }
        }

        private void inputTimesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            // Insert code to read the stream here.
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }

                label3.Visible = true;
                label3.Text = "20 times imported";
            }

        }

        private void inputBibsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form InputBibs = new InputBibs(this);
            InputBibs.Show();
            
        }

        private void inPlainTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<Participant> Participants = new List<Participant>();
            Participants.Add(new Participant("Jeff Huber", 120));
            Participants.Add(new Participant("Joe Bob", 121));
            Participants.Add(new Participant("Jay Brockman", 122));
            Participants.Add(new Participant("Alice Wonderland", 123));
            Participants.Add(new Participant("Barack Obama", 124));
            Participants.Add(new Participant("Kelly Johnson", 125));
            Participants.Add(new Participant("Amber Smith", 126));
            Participants.Add(new Participant("Luke Pond", 127));
            Participants.Add(new Participant("Amy Pond", 128));
            Participants.Add(new Participant("Alex Hisen", 129));
            Program.CurrentRace.Participants = Participants;

            List<string> Times = new List<string>();
            Times.Add("19:58");
            Times.Add("20:03");
            Times.Add("20:14");
            Times.Add("20:19");
            Times.Add("20:30");
            Times.Add("21:10");
            Times.Add("21:30");
            Times.Add("22:35");
            Program.CurrentRace.Times = Times;

            

            StreamWriter myStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = new StreamWriter(saveFileDialog1.FileName)) != null)
                {
                    myStream.WriteLine("Results of " + Program.CurrentRace.Name + "\n\n");

                    int place = 1;
                    foreach (int bib in Program.CurrentRace.Bibs)
                    {
                        Participant participant = null;

                        // Lookup participant by bib
                        foreach (Participant p in Program.CurrentRace.Participants)
                        {
                            if (p.Bib == bib)
                            {
                                participant = p;
                                break;
                            }
                        }

                        // Get time
                        string time = Program.CurrentRace.Times[place - 1];


                        // Write line
                        myStream.WriteLine(place.ToString() +
                                            "\t" +
                                            participant.Bib +
                                            "\t" +
                                            participant.Name +
                                            "\t\t" +
                                            time);



                        place++;
                    }


                    myStream.Close();
                }
            }


            ResultsMessage("Results saved to: " + saveFileDialog1.FileName.ToString());
        }
    }
}
